package top.wangjiu.reactive.rpc.encode;

import io.netty.buffer.ByteBuf;
import io.netty.channel.ChannelHandlerContext;
import io.netty.handler.codec.MessageToByteEncoder;
import io.protostuff.LinkedBuffer;
import io.protostuff.ProtostuffIOUtil;
import io.protostuff.runtime.RuntimeSchema;
import top.wangjiu.reactive.rpc.model.RpcRequest;

public class RpcRequestEncoder extends MessageToByteEncoder<RpcRequest> {

    private static RuntimeSchema<RpcRequest> schema = RuntimeSchema.createFrom(RpcRequest.class);

    @Override
    protected void encode(ChannelHandlerContext ctx, RpcRequest msg, ByteBuf out) throws Exception {
        byte[] bytes = ProtostuffIOUtil.toByteArray(msg, schema,LinkedBuffer.allocate(LinkedBuffer.DEFAULT_BUFFER_SIZE));
        out.writeInt(bytes.length);
        out.writeBytes(bytes);
    }
}
